
"use strict";
// Class definition

var riskWarningMeasuresAdd = function () {

    var eventHandler = function () {
        $(document).off('click', '.add-measures');
        $(document).on('click', '.add-measures', setIndex);

        $(document).off('click', '.delete-btn');
        $(document).on('click', '.delete-btn', setIndex);

        $(document).off('click', '.risk-warning-measures-form .clear-form');
        $(document).on('click', '.risk-warning-measures-form .clear-form', clearFormHandle);
    };
    
    // Private functions
    var initRepeater = function () {
        $('.k-repeater').each(function(){
            $(this).repeater({
                show: function () {
                    $(this).slideDown();
                },
                // Enable the option below to have a 2-step remove button
                // hide: function (deleteElement) {
                //     $(this).slideUp(deleteElement);
                // },
                isFirstItemUndeletable: true
            });
        });

        $('.k_selectpicker').selectpicker();
    };

    var setIndex = function () {
        $('.k-repeater').each(function (i, repeater) {
            var kRepeaterItems = $(repeater).find('.k-repeater__item');
            kRepeaterItems.each(function (index, item) {
                $(item).find('.span-text').text(index + 1 + '.');
            });
        });
    };

    var clearFormHandle = function() {
        $(".risk-warning-measures-form").clearForm();
        $('.k_selectpicker').selectpicker('val', '');
    };

    return {
        // public functions
        init: function() {
            initRepeater();
            eventHandler();
        }
    };
}();

jQuery(document).ready(function() {
    riskWarningMeasuresAdd.init();
});